#!/bin/sh


TZDHCP_MAIN_DHCPSTART=`uci get tz_mgr.main.TZ_DHCP_IP_BEGIN`
TZDHCP_MAIN_DHCPEND=`uci get tz_mgr.main.TZ_DHCP_IP_END`
TZDHCP_MAIN_DHCPLEASE=`uci get tz_mgr.main.TZ_DHCP_EXPIRE_TIME`
DHCP_MAIN_START=
DHCP_MAIN_END=
DHCP_MAIN_LIMIT=
DHCP_MAIN_DHCPLEASE=


if [ "${TZDHCP_MAIN_DHCPSTART}" != "" ]; then
	DHCP_MAIN_START=`echo ${TZDHCP_MAIN_DHCPSTART} | awk -F '.' '{print $4}'`
else
	DHCP_MAIN_START=100
fi

if [ "${TZDHCP_MAIN_DHCPEND}" != "" ]; then
	DHCP_MAIN_END=`echo ${TZDHCP_MAIN_DHCPEND} | awk -F '.' '{print $4}'`
else
	DHCP_MAIN_END=200
fi

DHCP_MAIN_LIMIT=`echo $((${DHCP_MAIN_END}-${DHCP_MAIN_START}))`

if [ "${TZDHCP_MAIN_DHCPLEASE}" != "" ]; then
	DHCP_MAIN_DHCPLEASE="$((${TZDHCP_MAIN_DHCPLEASE}/3600))h"
else
	DHCP_MAIN_DHCPLEASE=24h
fi




cat > /etc/config/dhcp <<EOF

config dnsmasq
        option domainneeded '1'
        option boguspriv '1'
        option filterwin2k '0'
        option localise_queries '1'
        option rebind_protection '1'
        option rebind_localhost '1'
        option local '/lan/'
        option domain 'lan'
        option expandhosts '1'
        option nonegcache '0'
        option authoritative '1'
        option readethers '1'
        option leasefile '/tmp/dhcp.leases'
        option resolvfile '/tmp/resolv.conf.auto'
        option localservice '1'

config dhcp 'lan'
        option interface 'lan'
        option start '${DHCP_MAIN_START}'
        option limit '${DHCP_MAIN_LIMIT}'
        option leasetime '${DHCP_MAIN_DHCPLEASE}'
        option dhcpv6 'server'
        option ra 'server'
        option ignore '1'

config dhcp 'wan'
        option interface 'wan'
        option ignore '1'

config odhcpd 'odhcpd'
        option maindhcp '0'
        option leasefile '/tmp/hosts/odhcpd'
        option leasetrigger '/usr/sbin/odhcpd-update'

EOF


